package com.woniuxy.qiantai.interceptors;

import com.woniuxy.qiantai.utils.CookieUtils;
import com.woniuxy.qiantai.utils.JwtUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class PermissionInterceptor implements HandlerInterceptor {
    //登录拦截器

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userToken2Cookie = CookieUtils.getUserToken2Cookie(request);
        if (StringUtils.isEmpty(userToken2Cookie)) {
        //如果userToken2Cookie为空 表明还没有登录
            request.getRequestDispatcher("/user/redirectLoginHtml").forward(request,response);
            return  false;
        }
        try {
            String account = JwtUtils.getAccount(userToken2Cookie);
        } catch (Exception e) {
            //如果报错 表明token已经过期
            request.getRequestDispatcher("/user/redirectLoginHtml").forward(request,response);
            return  false;
        }
        return true;
    }
}
